<template>
  <el-form v-show="showSearch" ref="queryRef" :model="queryParams" :inline="true">
    <el-form-item label="项目名称" prop="name">
      <el-input v-model="queryParams.name" placeholder="请输入项目名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      <el-button icon="Refresh" @click="resetQuery">重置</el-button>
    </el-form-item>
  </el-form>
  <el-row :gutter="10" class="mb8">
    <el-col :span="20">
      <el-button type="primary" plain icon="Plus" @click="handleAdd">新建</el-button>
      <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
    </el-col>
    <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
  </el-row>
  <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55" align="center" />
    <el-table-column label="项目名称" prop="name" min-width="180" />
    <el-table-column label="排序" prop="orderIndex" min-width="100" />
    <el-table-column label="操作" width="120" fixed="right">
      <template #default="scope">
        <el-button type="text" @click="updateRow(scope.row)">编辑</el-button>
        <el-button type="text" @click="removeRow(scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <pagination
    v-show="total > 0"
    v-model:page="queryParams.pageNum"
    v-model:limit="queryParams.pageSize"
    :total="total"
    @pagination="getList"
  />
  
  
  <!-- 项目编辑 -->
  <el-dialog v-model="open" :title="title" width="500px" append-to-body :close-on-click-modal="false">
    <el-form ref="dialogRef" :model="form" label-width="100px">
      <el-form-item label="项目名称" prop="name"><el-input v-model="form.name" placeholder="请输入项目名称" /></el-form-item>
      <el-form-item label="排序" prop="orderIndex"><el-input v-model="form.orderIndex" type="number" placeholder="请输入排序" /></el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="open=false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>

import { ref } from 'vue'
import { getCurrentInstance, onMounted } from '@vue/runtime-core'
import { getBaseProjectList, modifyBaseProject, delBaseProject } from '../../../../api/oam/basic'


const { proxy } = getCurrentInstance()
const showSearch = ref(true)
const loading = ref(true)
const queryParams = ref({})
const multiple = ref(true)
const tableData = ref([])
const total = ref(0)

const open = ref(false)
const form = ref({})
const title = ref('')

let selectedIds = []


function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 获取列表
function getList() {
  loading.value = true
  getBaseProjectList(queryParams.value).then(res => {
    tableData.value = res.rows
    total.value = res.total
    loading.value = false
  })
}


function handleAdd() {
  form.value = {}
  proxy.resetForm('dialogRef')

  open.value = true
  title.value = '新建项目'
}

function handleDelete() {
  proxy.$modal
    .confirm(`是否确认删除 ${selectedIds.length} 个项目？`)
    .then(function() {
      return delBaseProject(selectedIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
}

function handleSelectionChange(selection) {
  selectedIds = selection.map(item => item.id)
  multiple.value = !selection.length
}

function updateRow(row) {
  form.value = { ...row }
  open.value = true
  title.value = '编辑项目'
}

function removeRow(row) {
  proxy.$modal
    .confirm(`是否确认删除项目 ${row.name}`)
    .then(() => {
      return delBaseProject(row.id)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
}

function submitForm() {
  const { name, orderIndex } = form.value
  if (!name || !orderIndex) {
    proxy.$modal.msgError('请完善信息')
    return
  }

  modifyBaseProject(form.value).then(() => {
    getList()
    open.value = false
  })
}

onMounted(() => {
  getList()
})

</script>

<style scoped>

</style>
